package com.example.administrator.demonprocessdemo;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

import static android.content.ContentValues.TAG;
import static android.icu.text.Normalizer.NO;

public class LocalService extends Service {
    private LocalServiceBinder localBinder;
    private LocalServiceConnection localConn;
    private Timer timer;
    private int count;

    public LocalService() {
    }

    @Override
    public IBinder onBind(Intent intent) {

        return localBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if (localBinder == null){
            // LocalServiceBinder 是静态的内部类  要判空 否则会泄漏
            localBinder = new LocalServiceBinder();

        }

        localConn = new LocalServiceConnection();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setDefaults(Notification.DEFAULT_SOUND);
        builder.setContentTitle("我是曾令文");
        builder.setContentInfo("我是Info");
        builder.setWhen(System.currentTimeMillis());
        PendingIntent p = PendingIntent.getActivity(this,0,intent,0);
        builder.setContentIntent(p);
        Notification notification = builder.build();
        startForeground(startId,notification);
        startTimer();
        return Service.START_STICKY;

    }


    private void startTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.e(TAG, "run: Local  " + count );
                count++;
            }
        },100,100);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    class LocalServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG, "onServiceConnected: " + name  );
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //当进程被干掉的时候一定会走的方法
            Log.e(TAG, "onServiceDisconnected: " + name );
            startService(new Intent(LocalService.this, RemoteService.class));
            bindService(new Intent(LocalService.this, RemoteService.class), localConn, Context.BIND_IMPORTANT);

        }
    }

    class LocalServiceBinder extends IMyAidlInterface.Stub{

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }
    }
}
